<?php
namespace Frontend\Controller;
use Zend\Authentication\AuthenticationService;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Velacolib\Utility\Utility;
use Admin\Model\AdminTopicTable;
use Frontend\Helper\MobileDetect;
 use Zend\View\Helper;
use Zend\Session\Container;

class IndexController extends AbstractActionController
{
    public $flickr;
    protected $topicModel;
    protected $topiccatModel;

    public function onDispatch(\Zend\Mvc\MvcEvent $e)
    {
        //Utility::CheckLogin($this,'/login');
        $this->topicModel = Utility::UserGateway('Admin\Model\AdminTopicTable');
        $this->topiccatModel = Utility::UserGateway('Admin\Model\AdminCategoriesTable');
        // $this->flickr = new \Zend_Service_Flickr(FLICKR_API_KEY);

        $this->layout('frontend/layout');
        return parent::onDispatch($e);
    }

    public function init()
    {

    }

    public function indexAction()
    {


        $mobile_detect = new MobileDetect;
        $isMobile = $mobile_detect->isMobile();
        $isTablet = $mobile_detect->isTablet();
        $item_per_page = $this->params()->fromQuery('ipg');
        if ($item_per_page == 0) $item_per_page = 50;
        $data = $this->topicModel->fetch();
        $data->setCurrentPageNumber((int)$this->params()->fromQuery('page', 1));
        $data->setItemCountPerPage($item_per_page);
        $data->setPageRange(5);
        $view = $this->params()->fromQuery('view');
        $search_val = $this->params()->fromQuery('search_val');
        $search_cat = $this->params()->fromQuery('search_cat');
        $search_type = $this->params()->fromQuery('search_type');
        $search_loc = $this->params()->fromQuery('search_loc');
        $ipg = $this->params()->fromQuery('ipg');

        $cat = $this->topiccatModel->fetchAll(true);
        $cat->setItemCountPerPage(1000);

        if ($search_val != '' || $search_cat != '' || $search_type != '' || $search_loc != '' || $ipg != '') {
            $item_per_page = $this->params()->fromQuery('ipg');
            if ($item_per_page == 0) $item_per_page = 50;
            $data = $this->topicModel->searchFullText($search_val, $search_cat, $search_type, $search_loc);
            $data->setCurrentPageNumber((int)$this->params()->fromQuery('page', 1));
            $data->setItemCountPerPage($item_per_page);
            $return['search_val'] = array(
                'search_val' => $search_val,
                'search_cat' => $search_cat,
                'search_type' => $search_type,
                'search_loc' => $search_loc,
                'view' => $view,
                'ipg' => $ipg
            );
        }

        $return['search_val']['view'] = $view;
        if ($isMobile || $isTablet) {
            $session = new Container();
            if( !isset($session->mobile)){
                $session->mobile = false;
            }
            $return['session'] = $session;
            $template_path = 'frontend/index/index.phtml';
        } else {
            if ($view == 'list' || $view == '') {
                $template_path = 'frontend/index/index_list.phtml';
            } else {
                $template_path = 'frontend/index/index.phtml';
            }
        }



        $count_result =  count($data);
        ($count_result > 0) ? $result = true: $result = false;
        $return['title'] = DEFAULT_DESCRIPTION;
        $return['data'] = $data;
        $return['cat'] = $cat;
        $return['ipg'] = $item_per_page;
        $return['result'] = $result ;

        $view = new ViewModel($return);
        $view->setTemplate($template_path);
        return $view;
    }

    public  function friendlistAction()
    {

    }


}

?>
